package com.cwd.arm.domain.repository.impl;

import com.cwd.arm.domain.entity.ProjectMonthAmountTotalEntity;
import com.cwd.arm.infrastructure.dao.MonthAmountTotalDao;
import com.cwd.arm.infrastructure.po.MonthAmountTotalPo;
import com.cwd.arm.domain.entity.MonthAmountTotalEntity;
import com.cwd.arm.domain.repository.MonthAmountTotalRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Description:每月金额统计
* Created on 2021年11月16日
* @author  yan
*/
@Repository
public class MonthAmountTotalRepositoryImpl implements MonthAmountTotalRepository{

    @Autowired
    private MonthAmountTotalDao dao;

    @Override
    public void save(List<MonthAmountTotalEntity> entityList) {
        entityList.forEach(
                entity -> {
                    MonthAmountTotalPo po = dao.selectOne(entity.getFyYear(),entity.getFyMonth());
                    if(po == null){
                        dao.save(convertToPo(entity));
                    }else{
                        MonthAmountTotalEntity poBuildEntity = buildEntity(po);
                        poBuildEntity.calculate(entity);
                        dao.updateById(convertToPo(poBuildEntity));
                    }
                }
        );
    }

    @Override
    public MonthAmountTotalEntity buildEntity(ProjectMonthAmountTotalEntity projectMonthAmountTotalEntity) {
        MonthAmountTotalEntity entity = new MonthAmountTotalEntity();
        BeanUtils.copyProperties(projectMonthAmountTotalEntity,entity);
        return entity;
    }

    private MonthAmountTotalPo convertToPo(MonthAmountTotalEntity entity){
        MonthAmountTotalPo MonthAmountTotalPo = new MonthAmountTotalPo();
        BeanUtils.copyProperties(entity,MonthAmountTotalPo);
        return MonthAmountTotalPo;
    }

    private MonthAmountTotalEntity buildEntity(MonthAmountTotalPo po){
        MonthAmountTotalEntity entity = new MonthAmountTotalEntity();
        BeanUtils.copyProperties(po,entity);
        return entity;
    }


}
